package com.destination.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.destination.model.ConnUtil;

public class ShowClassRooms extends Activity {

	private ConnUtil cu=new ConnUtil();
	private Dialog dlg=null;
	private String strResult=null;
	private ListView lv_classrooms=null;
//	private classRoomsAsync async=null;
	private List<Map<String, String>> classrooms=new ArrayList<Map<String,String>>();
	
	private LinearLayout mainLayout=null;
	private LinearLayout showLayout=null;
	
	private int currentPage=1;
	private int lineSize=8;
	private int allRecorders=0;
	private int pageSize=1;
	private int lastItem=0;
	
	private TextView tv_loadInfo=null;
	private LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
	
	private SimpleAdapter simpleAdapter=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_classrooms);
		
		mainLayout=(LinearLayout)findViewById(R.id.layout_tab_classrooms);
		showLayout=new LinearLayout(this);
		tv_loadInfo=new TextView(this);
		tv_loadInfo.setText("正在加载中……");
		tv_loadInfo.setGravity(Gravity.CENTER);
		tv_loadInfo.setTextSize(20.0F);	
		showLayout.addView(tv_loadInfo,layoutParams);
		showLayout.setGravity(Gravity.CENTER);	
	    showAllData();		
	}
	
	private class OnScrollListenerImpl implements OnScrollListener{
		
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			// TODO Auto-generated method stub
			lastItem=firstVisibleItem+visibleItemCount-1;
		}
		
		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			// TODO Auto-generated method stub
			if(lastItem==simpleAdapter.getCount()&&
					currentPage<pageSize&&
					scrollState==OnScrollListener.SCROLL_STATE_IDLE){
				currentPage++;
				lv_classrooms.setSelection(lastItem);
				appendData();
				if(currentPage==pageSize)
					lv_classrooms.removeFooterView(showLayout);
			}
		}
	}
	
	private void appendData(){	
		try {
			List<Map<String, String>> currentRooms = parseClassRoomsMapJson(cu.getDisClassroomsInfo(currentPage, lineSize));
			classrooms.addAll(currentRooms);
			simpleAdapter.notifyDataSetChanged();
			
//			if(currentPage<pageSize)
//				lv_classrooms.addFooterView(showLayout);
//			else
//				lv_classrooms.removeFooterView(showLayout);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	private void showAllData(){
		
		lv_classrooms=new ListView(ShowClassRooms.this);		
		allRecorders=Integer.parseInt(cu.getDisClassroomsCountInfo().trim());
		pageSize=(allRecorders+currentPage-1)/lineSize+1;
		try {
			classrooms=parseClassRoomsMapJson(cu.getDisClassroomsInfo(currentPage, lineSize));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		simpleAdapter=new SimpleAdapter(ShowClassRooms.this, 
				classrooms, R.layout.classrooms_item, 
				new String[]{"class_id","class_name","class_type"},
				new int[]{R.id.txt_class_id,R.id.txt_class_name,R.id.txt_class_type});
		if(currentPage<pageSize)
			lv_classrooms.addFooterView(showLayout);
		else
			lv_classrooms.removeFooterView(showLayout);
		lv_classrooms.setAdapter(simpleAdapter);
		lv_classrooms.setOnScrollListener(new OnScrollListenerImpl());
		mainLayout.addView(lv_classrooms);
	}
	
	
//	private class classRoomsAsync extends AsyncTask<Integer, Integer, String>{
//		@Override
//		protected void onPreExecute() {
//			// TODO Auto-generated method stub
//			dlg=new AlertDialog.Builder(ShowClassRooms.this)
//				.setMessage("正在加载中……")
//				.create();
//			dlg.show();
//			super.onPreExecute();
//		}
//		@Override
//		protected String doInBackground(Integer... params) {
//			// TODO Auto-generated method stub
//			strResult=cu.getDisClassroomsInfo();
//			return strResult;
//		}
//		@Override
//		protected void onPostExecute(String result) {
//			// TODO Auto-generated method stub	
////			classrooms=new ArrayList<Map<String,String>>();
////			try {
////				classrooms=parseClassRoomsMapJson(result);
////			} catch (Exception e) {
////				// TODO Auto-generated catch block
////				e.printStackTrace();
////			}
////			=new SimpleAdapter(ShowClassRooms.this, 
////					classrooms, R.layout.classrooms_item, 
////					new String[]{"class_id","class_name","class_type"},
////					new int[]{R.id.txt_class_id,R.id.txt_class_name,R.id.txt_class_type});
////			lv_classrooms.setAdapter(simple);
//			Toast.makeText(ShowClassRooms.this, result, 3000).show();
//			dlg.dismiss();
//			super.onPostExecute(result);
//		}
//	}
	
//	@Override
//	protected void onResume() {
//		// TODO Auto-generated method stub
//		async=new classRoomsAsync();
//		async.execute(100);
//		super.onResume();
//	}
	
	public List<Map<String,String>> parseClassRoomsMapJson(String dataJson) throws Exception{
		
		List<Map<String,String>> classrooms=new ArrayList<Map<String,String>>();
		JSONArray jsonArr=new JSONArray(dataJson.toString().trim());
		for(int i=0;i<jsonArr.length();i++){		
			Map<String,String> classroom=new HashMap<String, String>();
			JSONObject jsonObject=jsonArr.getJSONObject(i);
			classroom.put("class_id",String.valueOf(jsonObject.getInt("class_id"))+":  ");
			classroom.put("class_name",jsonObject.getString("class_name"));
			classroom.put("class_type",jsonObject.getString("class_type"));
			classrooms.add(classroom);
		}
		return classrooms;
	}
}
